package com.dushiguang.findhoney.Fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.widget.ImageView;

import com.dushiguang.findhoney.R;
import com.dushiguang.findhoney.Utils.ImageHelper.SetImageResource;
import com.dushiguang.findhoney.View.Banner.BannerCircleIndicator;
import com.dushiguang.findhoney.View.Banner.ImageHandle;
import com.dushiguang.findhoney.View.Banner.ViewPagerOfBanner;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;

/**
 * Created by ex-dushiguang201 on 2015-12-01.
 */
@EFragment(R.layout.image_viewpager)
public class ViewPagerFragment extends Fragment {

    @ViewById(R.id.image_viewpager)
    ViewPagerOfBanner endlessViewpager;

    @ViewById(R.id.fixed_indicator)
    BannerCircleIndicator fixedIndicator;

    private FragmentActivity mContext;
    private boolean isAutoSwitch = false;                // 是否自动切换
    private int autoSwitchInterval = 2 * 1000;             // 自动切换间隔时间
    private ArrayList<String> imgUrls;
    private OnImgItemClickListener itemClickListener;
    private ImageView.ScaleType scaleType = ImageView.ScaleType.CENTER_CROP;
    private SetImageResource mSetImageResource;

    private int mLocalPic[];

    @AfterViews
    void afterView() {
        mSetImageResource = new SetImageResource();

        endlessViewpager.setData(mContext, imgUrls, mLocalPic, new ImageHandle() {

            @Override
            public void onItemClick(String url, int position) {
                if (null != itemClickListener) {
                    itemClickListener.onItemClick(position, url);
                }
            }

            @Override
            public void loadImage(ImageView photoIV, String url, int position) {
                photoIV.setScaleType(scaleType);
                //TODO 使用Picasso 从网络上下载图片
                mSetImageResource.getImageData(mContext, photoIV, url);
            }

            /**
             * 如果pic的长度不为0则使用App mipmap中的图片
             * @param photoIV
             * @param pic
             * @param position
             */
            public void loadLocalImage(ImageView photoIV, int pic, int position) {
                photoIV.setScaleType(scaleType);
                photoIV.setImageResource(pic);
            }

        });

        if(mLocalPic.length <= 0) {
            fixedIndicator.setCount(null == imgUrls ? 0 : imgUrls.size());
        } else {
            fixedIndicator.setCount(null == mLocalPic ? 0 : mLocalPic.length);
        }
        fixedIndicator.setViewPager(endlessViewpager);
        imgSwitch();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mContext = (FragmentActivity) activity;
    }

    /**
     * 设置显示图片的地址列表
     *
     * @param imgUrls
     */
    public void setImgUrls(ArrayList<String> imgUrls) {
        if (null == imgUrls || 0 == imgUrls.size()) {
            Log.v("ViewPagerFragment", " img urls is empty !!!");
            return;
        }
        this.imgUrls = imgUrls;
    }

    /**
     * 设置显示图片的地址列表
     *
     * @param picIds
     */
    public void setImgIds(int picIds[]) {
        if (null == picIds || 0 == picIds.length) {
            Log.v("ViewPagerFragment", " picIds is empty !!!");
            return;
        }
        this.mLocalPic = picIds;
    }

    /**
     * 设置图片显示发生变法listner
     *
     * @param pageChangeListener
     */
    public void setOnPageChangeListener(ViewPager.OnPageChangeListener pageChangeListener) {
        if (null != pageChangeListener) {
            fixedIndicator.setOnPageChangeListener(pageChangeListener);
        }
    }

    /**
     * 设置图片点击listener
     *
     * @param listener
     */
    public void setOnImgItemClickListener(OnImgItemClickListener listener) {
        this.itemClickListener = listener;
    }

    /**
     * 图片项点击回调接口
     *
     * @author zhangjiao
     */
    public static interface OnImgItemClickListener {
        /**
         * 图片项点击回调方法
         *
         * @param position 图片下标
         * @param imgUrl   图片地址
         */
        public void onItemClick(int position, String imgUrl);
    }

    /**
     * 设置图片是否自动切换,默认不自动切换
     *
     * @param isAutoSwitch
     */
    public void setIsAutoSwitch(boolean isAutoSwitch) {
        this.isAutoSwitch = isAutoSwitch;
        imgSwitch();
    }

    /**
     * 设置自动切换的间隔时间
     *
     * @param interval 间隔时间，单位ms,默认值为1000ms
     */
    public void setAutoSwitchInterval(int interval) {
        autoSwitchInterval = interval;
    }

    public ImageView.ScaleType getScaleType() {
        return scaleType;
    }

    public void setScaleType(ImageView.ScaleType scaleType) {
        this.scaleType = scaleType;
    }

    private final int ID_AUTO_SWITCH = 1;
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (ID_AUTO_SWITCH == msg.what && isAutoSwitch) {
                int maxIndex = imgUrls.size() - 1;
                int currIndex = endlessViewpager.getCurrentItem();
                endlessViewpager.setCurrentItem(currIndex == maxIndex ? 0
                        : ++currIndex, true);
                imgSwitch();
            }
        }

        ;
    };

    private void imgSwitch() {
        if (null != handler) {
            handler.sendEmptyMessageDelayed(ID_AUTO_SWITCH, autoSwitchInterval);
        }
    }
}
